<template>
    <div class="login-container">
        <el-form ref="loginFromRef" :model="logininfo" :rules="loginrules" class="login-form">
            <div class="title-container">
                <h3 class="title">Login</h3>
            </div>

            <el-form-item prop="username">
                <span class="svg-container">
                    <el-icon>
                        <Avatar />
                    </el-icon>
                </span>
                <el-input v-model="logininfo.username" placeholder="usernanme" type="text" name="username"></el-input>
            </el-form-item>

            <el-form-item prop="password">
                <span class="svg-container">
                    <el-icon>
                        <Key />
                    </el-icon>
                </span>
                <el-input v-model="logininfo.password" placeholder="password" name="password" v-bind:type='passwordType' />
                <span class="show-pwd" @click="onChangepwdType">
                    <el-icon>
                        <View v-if="passwordType === 'password'" />
                        <TurnOff v-if="passwordType === 'text'" />
                    </el-icon>
                </span>
            </el-form-item>
            <el-button @click="handlerLogin" type="primary" :loading="loading"
                style="width: 100%; margin-bottom: 30px">登录</el-button>
        </el-form>
    </div>
</template>

<script setup>
import { Avatar, Key, View, TurnOff } from "@element-plus/icons-vue";
import { ref } from 'vue';
import { useRouter } from 'vue-router'
import { useStore } from "vuex"

const logininfo = ref({
    username: '18612345678',
    password: '123456'
})
// 登录规则
const loginrules = {
    username: [
        { required: true, message: '请输入用户名', trigger: 'blur' }
    ],
    password: [
        { required: true, message: '请输入密码', trigger: 'blur' },
        { min: 6, max: 16, message: '密码长度6到16', trigger: 'blur' }
    ]
}
// 登录规则

// 密码的显示和隐藏
const passwordType = ref('password')
const onChangepwdType = () => {
    if (passwordType.value === 'password') {
        passwordType.value = 'text'
    } else {
        passwordType.value = 'password'
    }
    console.log(passwordType.value);
}
// 密码的显示和隐藏

// 登录动作处理
const router = useRouter()
const loading = ref(false)
const loginFromRef = ref(null)
const store = useStore()
const handlerLogin = () => {
    loginFromRef.value.validate(valid => {
        if (!valid) return
        loading.value = true
        store
            .dispatch('user/Login', logininfo.value)
            .then((res) => {
                console.log("login-----ing");
                // TODO: 登录后操作
                console.log(res);
                if (res.code == '1000') {
                    loading.value = false
                    router.push('/')
                }
            })
            .catch(err => {
                console.log(err)
                loading.value = false
            })
    })
}
// 登录动作处理
</script>

<style lang="scss" scoped>
::v-deep .el-input__wrapper {
    background: transparent;
    box-shadow: 0 0 0 0;
    width: 515px;
    position: relative;
    left: -33px;
    padding-left: 30px;
}

$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;
$cursor: #fff;

.login-container {
    min-height: 100%;
    width: 100%;
    background-color: $bg;
    overflow: hidden;

    .login-form {
        position: relative;
        width: 520px;
        max-width: 100%;
        padding: 160px 35px 0;
        margin: 0 auto;
        overflow: hidden;

        ::v-deep .el-form-item {
            border: 1px solid rgba(255, 255, 255, 0.1);
            background: rgba(0, 0, 0, 0.1);
            border-radius: 5px;
            color: #454545;
        }

        ::v-deep .el-input {
            display: inline-block;
            height: 47px;
            width: 85%;

            input {
                background: transparent;
                border: 0px;
                // -webkit-appearance: none;
                border-radius: 0px;
                padding: 12px 5px 12px 15px;
                color: $light_gray;
                height: 47px;
                caret-color: $cursor;
            }
        }
    }


    .svg-container {
        padding: 6px 5px 6px 15px;
        color: $dark_gray;
        vertical-align: middle;
        display: inline-block;
    }

    .title-container {
        position: relative;

        .title {
            font-size: 26px;
            color: $light_gray;
            margin: 0px auto 40px auto;
            text-align: center;
            font-weight: bold;
        }
    }

    .show-pwd {
        position: absolute;
        right: 10px;
        top: 7px;
        font-size: 16px;
        color: $dark_gray;
        cursor: pointer;
        user-select: none;
    }
}
</style>
